/**
 * Copyright (c) 2014 zhenrongbao.com, Inc. All Rights Reserved
 * @author: jiangshuguang
 * @Date  : 14-11-5
 * @Time  : 下午3:30
 * @brief :
 */
(function () {
    var Pagination = require('common:widget/ui/pagination/pagination.js').Pagination,
        util = require('common:widget/ui/libs/util.js').util,
        $page = $(".page-assetsterm"),
        $construct = $page.find('.construct'),
        $profit = $page.find('.profit'),
        $rechargeList = $page.find('.recharge-list');

    P.createPage({
        _data:{
            pid:0,
            status:0
        },

        _create:function(){
            var me = this,pagination;

            //初始化页码
            pagination = new Pagination({
                container:'.recharge-pagination',
                perCount:10,
                cur:1,
                onRefresh:function(curPage,callback){
                    $.getJSON('/user/gettermlist',{
                        'ps':10,
                        'pn':curPage,
                        'status':me.data('status')
                    },function(ret,status){
                        if(ret.error_no == 0 && ret.data){
                            callback(ret.data.count);
                            me._createTableList(ret.data.list);
                        }
                    });
                }
            });

            this.data({
                'pagination':pagination
            });
            var data = {
                total:parseFloat(P.data("fenjibao2")['total']),
                waiting_amount:parseFloat(P.data("fenjibao2")['waiting_amount']),
                amount:parseFloat(P.data("fenjibao2")['amount']),
                profit:parseFloat(P.data("fenjibao2")['profit']),
                freeze:parseFloat(P.data("fenjibao2")['freeze']),
                actual_amount: parseFloat(P.data("fenjibao2")['actual_amount']),
                actual_profit:parseFloat(P.data("fenjibao2")['actual_profit'])
            };
            this.createCycle($page.find('.fenjibao2-assets-cycle'),data);

             data = {
                 total:parseFloat(P.data("fenjibao3")['total']),
                 waiting_amount:parseFloat(P.data("fenjibao3")['waiting_amount']),
                 amount:parseFloat(P.data("fenjibao3")['amount']),
                 profit:parseFloat(P.data("fenjibao3")['profit']),
                 freeze:parseFloat(P.data("fenjibao3")['freeze']),
                 actual_amount: parseFloat(P.data("fenjibao3")['actual_amount']),
                 actual_profit:parseFloat(P.data("fenjibao3")['actual_profit'])
            };

            this.createCycle($page.find('.fenjibao3-assets-cycle'),data);
        },

        _init:function(){
            var pagination = this.data('pagination'),
                me = this;
            $rechargeList.find('.panel-tab li').click(function(){
                $rechargeList.find('.list-table tbody').html("");
                $rechargeList.find('.panel-tab li').removeClass('active');
                $(this).addClass('active');
                me.data('status',$(this).attr('data-status'));
                var text = $(this).attr('data-status')==0?'预期收益':'实际收益';
                $rechargeList.find('.list-table .profit').text(text);
                pagination.hide();
                pagination.initPage(1);
            });
        },


        //购买列表
        _createTableList:function(list){
            var status = this.status;
            $rechargeList.find('.list-table tbody').html((function(){
                var html = "";
                $.each(list,function(k,v){
                    html+='' +
                        '<tr>' +
                        '<td>'+ v.serial_id+'</td>' +
                        '<td>'+util.toTime(v.time*1000)+'</td>' +
                        '<td>'+ P.productMap[v.pid]+'</td>' +
                        '<td>'+(v.pid==2?'一个月':'三个月')+'</td>' +
                        '<td>'+ mathUtil.formatAmount(v.amount,4)+'</td>' +
                        '<td>'+util.toTime(v.end_time*1000)+'</td>' +
                            (status?'<td>￥'+ mathUtil.formatAmount(v.actual_profit,4)+'</td>':
                                '<td>￥'+ mathUtil.formatAmount(v.expect_profit,4)+'</td>')+
                        '</tr>' +
                        ''
                });
                return html;
            })());
        },


        createCycle:function($container,data){

            $container.highcharts({
                chart : {
                    backgroundColor:'rgba(0,0,0,0)',
                    borderColor:"#AAA"
                },

                credits : {
                    enabled:false
                },

                title: {
                    userHTML : true,
                    text: '<div style="color:#669;font-style:italic;font-size:30px">'+
                        '￥' + mathUtil.formatAmount(data.total,4)+ '</div>',
                    color:'#FFF',
                    align: 'center',
                    verticalAlign: 'middle',
                    y: -10
                },
                subtitle : {
                    userHTML : true,
                    text: '<div style="font-size:14px;padding-top:30px;color:#336">资金总额</div>',
                    verticalAlign:'middle',
                    y:20
                },
                tooltip: {
                    pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
                },
                legend: {
                    borderColor: '#909090'
                },
                plotOptions: {
                    pie: {
                        borderWidth:"0px",
                        size:"75%",
                        dataLabels: {
                            enabled: true
                        }
                    }
                },

                series: [{
                    type: 'pie',
                    name: '占比',
                    innerSize: '100%',
                    data :[
                        {
                            name:'当前持有本金',
                            y:parseFloat(data.actual_amount),
                            dataLabels : {
                                enabled:false
                            },
                            color:'#f93'
                        },
                        {
                            name:'当前持有收益',
                            y:parseFloat(data.actual_profit),
                            dataLabels : {
                                enabled:false
                            },
                            color:'#ef8f30'
                        },
                        {
                            name:'已到期本金',
                            y:parseFloat(data.amount),
                            dataLabels : {
                                enabled:false
                            },
                            color:'#df852d'
                        },
                        {
                            name:'已到期收益',
                            y:parseFloat(data.profit),
                            dataLabels : {
                                enabled:false
                            },
                            color:'#dfb225'
                        },
                        {
                            name:'排队中',
                            y:parseFloat(data.waiting_amount),
                            dataLabels : {
                                enabled:false
                            },
                            color:'#efbf27'
                        },
                        {
                            name:'提现中',
                            y:parseFloat(data.freeze),
                            dataLabels : {
                                enabled:false
                            },
                            color:'#efc70d'
                        }
                    ]
                }]
            });
        }
    });
})();